package com.drp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.drp.bean.*;
import com.drp.service.impl.*;
import jdk.vm.ci.amd64.AMD64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ren
 * @since 2022-03-16
 */
@RestController
@RequestMapping("/market")
public class MarketController {
    @Autowired
    private ClientServiceImpl clientServiceImpl;
    @Autowired
    private MarketServiceImpl marketServiceImpl;
    @Autowired
    private GoodsServiceImpl goodsServiceImpl;
    @Autowired
    private KindServiceImpl kindServiceImpl;
    @Autowired
    private UserServiceImpl userServiceImpl;
    @RequestMapping("/market2")
    @ResponseBody
    public List<Market2> getMarketList(){

        return marketServiceImpl.selectMarket();
    }
    @RequestMapping("/client")
    @ResponseBody
    public List<Client> getClient(){
        return clientServiceImpl.list();
    }
    @RequestMapping("/goods")
    @ResponseBody
    public List<Goods> getGoods(){
        QueryWrapper<Goods> queryWrapper=new QueryWrapper<>();
        queryWrapper.gt("g_number",0);
        List<Goods> list = goodsServiceImpl.list(queryWrapper);
        return list;
    }
    @RequestMapping("/add")
    @ResponseBody
    public boolean addMarket(@RequestParam("id") String id,@RequestParam("number") String number){
        boolean b=false;
        int number1=0;
        if(number!=null&&!number.equals("")){
            number1=Integer.parseInt(number);
        }
        Goods byId = goodsServiceImpl.getById(id);
        Market2 market2=new Market2();
        market2.setM_factory(byId.getGFactory());
        market2.setM_name(byId.getGName());
        market2.setM_sole(byId.getGSole()+"");
        market2.setM_number(number1+"");
        List<Market2> market2s = marketServiceImpl.selectMarket();
        for (int i = 0; i < market2s.size(); i++) {
            Market2 market21 = market2s.get(i);
            if(market21.getM_name().equals(market2.getM_name())){
                market21.setM_number(Integer.parseInt(market21.getM_number())+Integer.parseInt(market2.getM_number())+"");
                boolean b1 = marketServiceImpl.updateMarket2(market21.getM_id() + "", market21.getM_number());
                System.out.println("b1 = " + b1);
                return b1;
            }
        }
        b= marketServiceImpl.insertMarket2(market2);
        System.out.println("market2s = " + market2s);
        return b;
    }
    @RequestMapping("/look")
    @ResponseBody
    public List<Goods> lookGoods(@RequestParam("name") String name1,@RequestParam("kind") String kind){
        List<Goods> goods = marketServiceImpl.selectGoods(name1, kind);
        return goods;
    }
    @RequestMapping("/addNumber")
    @ResponseBody
    public boolean addNumber(@RequestParam("id") String id,@RequestParam("number")String number){
        int number1=0;
        if(number!=null&&!number.equals("")){
            number1=Integer.parseInt(number);
        }
        boolean b = marketServiceImpl.updateMarket2byId(id, number1 + "");
        return b;
    }
    @RequestMapping("/del")
    @ResponseBody
    public boolean del(@RequestParam("id") String id){
        boolean b = marketServiceImpl.delMarket2ById(id);
        return b;
    }
    @RequestMapping("/out")
    @ResponseBody
    public boolean out(@RequestParam("client") String client,@RequestParam("user") String user){
        QueryWrapper<Client> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("c_name",client);
        Client one = clientServiceImpl.getOne(queryWrapper);
        QueryWrapper<User> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("u_name", user);
        User one1 = userServiceImpl.getOne(queryWrapper1);
        List<Market2> market2s = marketServiceImpl.selectMarket();
        boolean save=false;
        for (int i = 0; i < market2s.size(); i++) {
            Market2 market2 = market2s.get(i);
            QueryWrapper<Goods> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("g_name",market2.getM_name());
            Goods one2 = goodsServiceImpl.getOne(queryWrapper2);
            Integer gNumber = one2.getGNumber();
            Integer m_number =Integer.parseInt(market2.getM_number());
            if(gNumber<m_number){
                gNumber=0;
            }else {
                gNumber=gNumber-m_number;
            }
            one2.setGNumber(gNumber);
            Market market=new Market();
            market.setFkClientid(one.getCId());
            market.setFkGoodsid(one2.getGId());
            market.setFkUserid(one1.getUId());
            market.setMNumber(Integer.parseInt(market2.getM_number()));
            double v = Double.parseDouble(market2.getM_sole());
            double x=v*market.getMNumber();
            market.setMAmount(x);
            Date date=new Date();
            SimpleDateFormat SimpleDateFormatsdf= new SimpleDateFormat("yyyy-MM-dd");
            Date nowDate= new Date();
            String format = SimpleDateFormatsdf.format(nowDate);
            market.setMTime(format);
            save = marketServiceImpl.save(market);
            save= goodsServiceImpl.updateById(one2);
        }
        boolean b=false;
        if(save){
            b = marketServiceImpl.delMarket2();
        }
        return b;
    }
}
